package com.yixia.pea.mapper;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.conditions.update.LambdaUpdateWrapper;
import com.yixia.common.utils.StringUtils;
import com.yixia.pea.constant.RatingStatus;
import com.yixia.pea.domain.credit.PeaHzBase;
import com.baomidou.mybatisplus.core.mapper.BaseMapper;
import com.yixia.pea.domain.dto.PeaHzBaseDto;
import org.apache.ibatis.annotations.Mapper;

import java.util.Arrays;
import java.util.List;

/**
* @author 35038
* @description 针对表【pea_hz_base】的数据库操作Mapper
* @createDate 2023-09-01 08:07:04
* @Entity com.yixia.pea.domain.credit.PeaHzBase
*/
@Mapper
public interface PeaHzBaseMapper extends BaseMapper<PeaHzBase> {

    // 身份证的唯一
    default PeaHzBase checkIdCardNumUnique(String hzIdCardNumber){
        if(StringUtils.isNotNull(hzIdCardNumber)) {
            LambdaQueryWrapper<PeaHzBase> queryWrapper = new LambdaQueryWrapper<>();
            queryWrapper.eq(PeaHzBase::getHzIdCardNumber, hzIdCardNumber);
            return this.selectOne(queryWrapper);
        }else {
            return null;
        }
    }

    // 手机号的唯一
    default PeaHzBase checkPhoneNumberNumUnique(String phoneNumber){
        if(StringUtils.isNotNull(phoneNumber)) {
            LambdaQueryWrapper<PeaHzBase> queryWrapper = new LambdaQueryWrapper<>();
            queryWrapper.eq(PeaHzBase::getHzPhoneNumber, phoneNumber);
            return this.selectOne(queryWrapper);
        }else {
            return null;
        }
    }


    // 分页查询
    List<PeaHzBaseDto> selectListPage(PeaHzBaseDto peaHzBaseDto);

    /**
     * 开始进行评分
     * @param hzId 户主ID
     * @return 返回结果
     */
    default int startScoring(Long hzId){
        LambdaUpdateWrapper<PeaHzBase> updateWrapper = new LambdaUpdateWrapper<>();
        updateWrapper.set(PeaHzBase::getRatingStatus, RatingStatus.SCORING).eq(PeaHzBase::getHzId, hzId);
        return this.update(null, updateWrapper);
    }

    // 将评分状态改成未评分
    default int resfreshRatingStatus(Long[] hzIds){
        List<Long> list = Arrays.asList(hzIds);
        LambdaUpdateWrapper<PeaHzBase> updateWrapper = new LambdaUpdateWrapper<>();
        updateWrapper.set(PeaHzBase::getRatingStatus, RatingStatus.NOT_STARTED).in(PeaHzBase::getHzId, list);
        return this.update(null, updateWrapper);
    }
}




